package cn.kjcoder.redis.task;

import cn.kjcoder.redis.config.Constants;
import cn.kjcoder.redis.domain.Content;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/23
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Service
@Slf4j
public class InitService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *模拟直播间的数据
     */
    @PostConstruct
    public void refreshRoom(){
        List<Content> contents=new ArrayList<>();
        //模拟直播100房间号 的弹幕数据
        String  key= Constants.ROOM_KEY+100;
        Random rand = new Random();
        for(int i=1;i<=20;i++){
            Content content=new Content();
            int id= rand.nextInt(1000);
            content.setUserId(Long.valueOf(id));

            int temp= rand.nextInt(100);
            content.setContent("发表"+temp);

            long time=System.currentTimeMillis()/1000;
            this.redisTemplate.opsForZSet().add(key,content,time);

            log.debug("模拟直播间100的发言弹幕数据={}",content);
        }
    }
}
